// ==UserScript==
// @name              豆瓣顶贴
// @version           1.2.1.1
// @icon              https://img3.doubanio.com/favicon.ico
// @description       豆瓣顶贴
// @author            custom
// @match             *://www.douban.com/*
// ==/UserScript==
//dynamicLoadCss('https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css');
//页面id对象
function Face(){
	this.id = {};
    this.config = {};
    this.time;
    this.commentTimer;
	this.view = 
	'<div class="main">'+
		//图标
		'<div class="iconFace" id="icon_face_mainFace" style="width:50px;height:50px;"><img src=""></div>'+
		//总界面界面
		'<div id="in_face_mainFace" style="width:350px;background: #ffc;display:none">'+
			//导航按钮
			'<div id="nav_face_mainFace">'+
				'<button id="nav_btn_home" type="button" class="btn btn-default">首页</button>'+
				'<button id="nav_btn_add" type="button" class="btn btn-default">添加</button>'+
				'<button id="nav_btn_set" type="button" class="btn btn-default">设置</button>'+
			'</div>'+
			//homeface
			'<div style="padding-left:20px;padding-right:20px;padding-bottom:20px;" id="home_face_mainFace">'+
				//倒计时文本模块
				'<div class="home_face_countdownFace" style="text-align: center;padding-top:20px;padding-bottom:20px;border:1px dashed #00f;margin-top:10px">'+
					'<span><font size="6" color="#f00">倒计时：</font><font id="home_tv_countdown" size="6" color="#f00">未开始</font></span>'+
				'</div>'+
				//开始暂停按钮模块
				'<div class="home_face_runFace" style="text-align: center">'+
					'<input style="margin-right:30px;margin-top:20px" class="btn btn-default" type="button" value="开始" id="home_btn_start">'+
					'<input style="margin-right:30px;margin-top:20px" class="btn btn-default" type="button" value="终止" id="home_btn_pause">'+
				'</div>'+
				//展示信息模块
				'<div class="home_face_infoFace" style="height:100px;border:1px solid #ccc;padding-bottom:5px;margin-top:10px">'+
					'<span style="margin:0px">当前执行的账号：</span><span id="home_tv_account">无</span></br>'+
					//'<span style="margin:0px">执行次数：</span><span id="runTime">无</span></br>'+
					//'<span style="margin:0px">间隔时间：</span><span id="timeText">无</span></br>'+
					//'<span style="margin:0px">目标url：</span><span id="targetUrl">无</span></br>'+
					'<span style="margin:0px">评论内容：</span><span id="home_tv_comment">无</span></br>'+
					'<span style="margin:0px">目标帖子url</span><span id="home_tv_article">无</span></br>'+
					'<span style="margin:0px">暂停开始时间：</span><span id="home_tv_stoptime_start">无</span></br>'+
					'<span style="margin:0px">暂停结束时间：</span><span id="home_tv_stoptime_end">无</span></br>'+
				'</div>'+
			'</div>'+
			//添加界面
			'<div style="padding-left:20px;padding-right:20px;padding-bottom:20px;display:none" id="add_face_mainFace">'+
				//添加用户界面
				'<div class="add_face_accountFace">'+
					//展示用户界面模块
					'<div class="add_face_accountList" style="text-align: center;">'+
						'<span>账号列表</span></br>'+
						'<ul id="add_face_accountul" style="padding:10px;border:1px solid #ccc;padding-bottom:5px">'+
						'</ul>'+
					'</div>'+
					//添加用户模块
					'<div class="add_face_addAccountFace style="text-align: center;">'+
						'<p style="margin:0px">添加账号</p>'+
						'<div class="add_face_showAddAccount" style="padding:10px;border:1px solid #ccc">'+
							'账号：<input class="form-control" name="add_et_account" id="add_et_account"></br>'+
							'密码：<input class="form-control" name="add_et_token" id="add_et_token"></br>'+
							'<input style="margin-top:10px;margin-right:20px" class="btn btn-default" type="button" id="add_btn_account_add" value="添加">'+
							'<input style="margin-top:10px;margin-right:20px" class="btn btn-default" type="button" id="add_btn_account_delete" value="删除账号">'+
						'</div>'+
					'</div>'+
				'</div>'+
				//添加评论界面
				'<div class="add_face_commentFace">'+
					//展示评论模块
					'<div class="add_face_commentList" style="text-align: center;">'+
						'<span>评论列表</span></br>'+
						'<ul id="add_face_commentul" style="padding:10px;border:1px solid #ccc;padding-bottom:5px">'+
						'</ul>'+
					'</div>'+
					//添加评论模块
					'<div class="add_face_addComment" style="text-align: center;">'+
						'<p style="margin:0px">添加评论</p>'+
						'<div class="add_face_showComment" style="padding:10px;border:1px solid #ccc">'+
							'评论：<input class="form-control" name="add_et_comment" id="add_et_comment"></br>'+
							'<input style="margin-top:10px;margin-right:20px" class="btn btn-default" type="button" id="add_btn_comment_add" value="添加">'+
							'<input style="margin-top:10px;margin-right:20px" class="btn btn-default" type="button" id="add_btn_comment_delete" value="删除评论">'+
						'</div>'+
					'</div>'+
				'</div>'+
				//添加帖子界面
				'<div class="add_face_addArticleFace">'+
					//展示帖子模块
					'<div class="add_face_articleList" style="text-align: center;">'+
						'<span>账号列表</span></br>'+
						'<ul id="add_face_articleul" style="padding:10px;border:1px solid #ccc;padding-bottom:5px">'+
						'</ul>'+
					'</div>'+
					//添加帖子模块
					'<div class="add_face_addArticle" style="text-align: center;">'+
						'<p style="margin:0px">添加账号</p>'+
						'<div class="article" style="padding:10px;border:1px solid #ccc">'+
							'帖子url：<input class="form-control" name="add_et_article" id="add_et_article"></br>'+
							'<input style="margin-top:10px;margin-right:20px" class="btn btn-default" type="button" id="add_btn_article_add" value="添加">'+
							'<input style="margin-top:10px;margin-right:20px" class="btn btn-default" type="button" id="add_btn_article_delete" value="删除帖子">'+
						'</div>'+
					'</div>'+
				'</div>'+
			'</div>'+
			//设置界面
			'<div style="padding-left:20px;padding-right:20px;padding-bottom:20px;display:none" id="set_face_mainFace">'+
				//设置信息总界面
				'<div class="set_face_setInfo">'+
					'<p style="margin:0px">设置信息</p>'+
                    //设置每次评论间隔时间
					'<div class="input-group">'+
                        '<span>发布评论间隔时间：</span>'+
						'<input type="number" id="set_et_run_interval_time" class="form-control" placeholder="输入发布评论间隔时间">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_run_interval_time" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置当前账号序号
					'<div class="input-group">'+
                        '<span>设置当前账号序号：</span>'+
						'<input type="number" id="set_et_account_num" class="form-control" placeholder="输入账号序号">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_account_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置账号执行次数
					'<div class="input-group">'+
                        '<span>设置账号执行次数：</span>'+
						'<input type="number" id="set_et_account_run_num" class="form-control" placeholder="输入账号执行次数">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_account_run_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置账号更换次数
					'<div class="input-group">'+
                        '<span>设置账号更换次数：</span>'+
						'<input type="number" id="set_et_account_change_num" class="form-control" placeholder="输入账号更换次数">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_account_change_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置当前评论序号
					'<div class="input-group">'+
                        '<span>设置当前评论序号：</span>'+
						'<input type="text" id="set_et_comment_num" class="form-control" placeholder="输入当前评论序号">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_comment_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置评论执行次数
					'<div class="input-group">'+
                        '<span>设置评论执行次数：</span>'+
						'<input type="text" id="set_et_comment_run_num" class="form-control" placeholder="输入评论执行次数">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_comment_run_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置评论更换次数
					'<div class="input-group">'+
                        '<span>设置评论更换次数：</span>'+
						'<input type="text" id="set_et_comment_change_num" class="form-control" placeholder="输入评论更换次数">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_comment_change_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置当前帖子序号
					'<div class="input-group">'+
                        '<span>设置当前帖子序号：</span>'+
						'<input type="number" id="set_et_article_num" class="form-control" placeholder="输入当前帖子序号">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_article_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置帖子执行次数
					'<div class="input-group">'+
                        '<span>设置帖子执行次数：</span>'+
						'<input type="number" id="set_et_article_run_num" class="form-control" placeholder="输入帖子执行次数">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_article_run_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置帖子更换次数
					'<div class="input-group">'+
                        '<span>设置帖子更换次数：</span>'+
						'<input type="number" id="set_et_article_change_num" class="form-control" placeholder="输入帖子更换次数">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_article_change_num" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
                    //设置暂停的开始时间
					'<div class="input-group">'+
                        '<span>设置暂停的开始时间：</span>'+
						'<input type="number" id="set_et_pause_start_hour" class="form-control" style="width:35px" placeholder="时">'+
                        '<span>：</span>'+
                        '<input type="number" id="set_et_pause_start_minute" class="form-control" style="width:35px" placeholder="分">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_pause_start" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
					//设置暂停的结束时间
					'<div class="input-group">'+
                        '<span>设置暂停的结束时间：</span>'+
                        '<input type="number" id="set_et_pause_end_hour" class="form-control" style="width:35px" placeholder="时">'+
                        '<span>：</span>'+
                        '<input type="number" id="set_et_pause_end_minute" class="form-control" style="width:35px" placeholder="分">'+
						'<span class="input-group-btn">'+
							'<button id="set_btn_pause_end" class="btn btn-default" type="button">Go!</button>'+
						'</span>'+
					'</div>'+
				'</div>'+
			'</div>'+
		'</div>'+
	'</div>';
	this.addView = function(parent){
		//将view这个视图添加到parent节点上
		//创建节点
		var div=document.createElement('div');
		div.innerHTML = this.view;
      div.style.position = "fixed";
      //div.style.display = 'inline';
      div.style.float = 'left';
      div.style.top = "20px";
      div.style.width = "350px";
      div.style.zIndex = "9999";
        //console.log("view:",this.view);
      parent.appendChild(div);
	};
    this.configView = function(){
        console.log("configviewstart");
		//设置各个按钮的点击事件
		//1.设置鼠标进入图标事件
		this.id.face.icon_face_mainFace.addEventListener("mouseenter",function(e){
            //console.log("id:",this);
			this.id.face.in_face_mainFace.style.display = 'block';
			this.id.face.icon_face_mainFace.style.display  = 'none';
		}.bind(this));
		//2.设置鼠标移出主界面事件
		this.id.face.in_face_mainFace.addEventListener("mouseleave",function(e){
			this.id.face.icon_face_mainFace.style.display = 'block';
			this.id.face.in_face_mainFace.style.display = 'none';
		}.bind(this));
		//3.设置首页按钮点击事件
		this.id.nav.btn_home.addEventListener("click",function(e){
			var isshow = this.id.face.home_face_mainFace.style.display;
			if(isshow == 'block'){
				return;
			}
            this.showHomeInfo();
			this.id.face.home_face_mainFace.style.display = 'block';
			this.id.face.add_face_mainFace.style.display = 'none';
			this.id.face.set_face_mainFace.style.display = 'none';
		}.bind(this));
		//4.设置添加按钮点击事件
		this.id.nav.btn_add.addEventListener("click",function(e){
			var isshow = this.id.face.add_face_mainFace.style.display;
			if(isshow == 'block'){
				return;
			}
			this.id.face.home_face_mainFace.style.display = 'none';
			this.id.face.add_face_mainFace.style.display = 'block';
			this.id.face.set_face_mainFace.style.display = 'none';
            //展示列表
            this.refreshAccountList();
            this.refreshCommentList();
            this.refreshArticleList();
		}.bind(this));
		//5.设置设置按钮点击事件
		this.id.nav.btn_set.addEventListener("click",function(e){
			var isshow = this.id.face.set_face_mainFace.style.display;
			if(isshow == 'block'){
				return;
			}
            this.showSetInfo();
			this.id.face.home_face_mainFace.style.display = 'none';
			this.id.face.add_face_mainFace.style.display = 'none';
			this.id.face.set_face_mainFace.style.display = 'block';
		}.bind(this));
        //6.添加账号事件
        this.id.add.btn_account_add.addEventListener("click",function(e){
            //获取账号和token
            var account = this.id.add.et_account.value;
            var token = this.id.add.et_token.value;
            if(account == '' || token == ''){
                alert("不能为空");
                return;
            }
			//添加账号，本地存储，当前对象
			this.config.config_account.accounts.push({"account":account,"token":token});
            this.setLocalConfigByConfig();
            //更新账号列表
            this.refreshAccountList();
            //清空编辑框
            this.id.add.et_account.value = '';
            this.id.add.et_token.value = '';
            //alert("添加成功");
		}.bind(this));
        //7.添加评论事件
        this.id.add.btn_comment_add.addEventListener("click",function(e){
            //获取账号和token
            var comment = this.id.add.et_comment.value;
            if(comment == ''){
                alert("不能为空");
                return;
            }
			//添加账号，本地存储，当前对象
			this.config.config_comment.comments.push(comment);
            this.setLocalConfigByConfig();
            //更新账号列表
            this.refreshCommentList();
            //alert("添加成功");
            //清空编辑框
            this.id.add.et_comment.value = '';
		}.bind(this));
        //8.添加目标url内容
        this.id.add.btn_article_add.addEventListener("click",function(e){
            //获取账号和token
            var article = this.id.add.et_article.value;
            if(article == ''){
                alert("不能为空");
                return;
            }
			//添加账号，本地存储，当前对象
			this.config.config_article.articles.push(article);
            this.setLocalConfigByConfig();
            //更新账号列表
            this.refreshArticleList();
            //alert("添加成功");
            //清空编辑框
            this.id.add.et_article.value = '';
		}.bind(this));
        //console.log(1);
        //设置评论间隔时间
        this.id.set.btn_run_interval_time.addEventListener("click",function(e){
            var run_interval_time = this.id.set.et_run_interval_time.value;
            if(run_interval_time == ''){
                alert("不能为空");
                return;
            }
            //console.log("run_interval_time:",run_interval_time);
            this.config.run_interval_time = run_interval_time;
            this.time = run_interval_time;
            this.setLocalConfigByConfig();
        }.bind(this));
        //设置当前账号（通过序号）
        this.id.set.btn_account_num.addEventListener("click",function(e){
            //获取账号当前账号序号
            var account_num = this.id.set.et_account_num.value;
            if(account_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_account.account_num = account_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_account_num.value = '';
		}.bind(this));
        //console.log(1.1);
        //设置当前账号账号执行次数
        this.id.set.btn_account_run_num.addEventListener("click",function(e){
            //获取账号当前账号序号
            var account_run_num = this.id.set.et_account_run_num.value;
            if(account_run_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_account.account_run_num = account_run_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_account_run_num.value = '';
		}.bind(this));
        //console.log(1.2);
        //设置当前账号账号执行次数
        this.id.set.btn_account_change_num.addEventListener("click",function(e){
            //获取账号当前账号序号
            var account_change_num = this.id.set.et_account_change_num.value;
            if(account_change_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_account.account_change_num = account_change_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_account_change_num.value = '';
		}.bind(this));
        //console.log(2);
        //设置当前评论（通过序号）
        this.id.set.btn_comment_num.addEventListener("click",function(e){
            //获取评论当前账号序号
            var comment_num = this.id.set.et_comment_num.value;
            if(comment_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_comment.comment_num = comment_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_comment_num.value = '';
		}.bind(this));
        //设置当前评论执行次数
        this.id.set.btn_comment_run_num.addEventListener("click",function(e){
            //获取账号当前评论序号
            var comment_run_num = this.id.set.et_comment_run_num.value;
            if(comment_run_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_comment.comment_run_num = comment_run_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_comment_run_num.value = '';
		}.bind(this));
        //设置当前评论执行次数
        this.id.set.btn_comment_change_num.addEventListener("click",function(e){
            //获取账号当前评论序号
            var comment_change_num = this.id.set.et_comment_change_num.value;
            if(comment_change_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_comment.comment_change_num = comment_change_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_comment_change_num.value = '';
		}.bind(this));
        //console.log(3);
        //设置当前帖子url（通过序号）
        this.id.set.btn_article_num.addEventListener("click",function(e){
            //获取当前帖子url序号
            var article_num = this.id.set.et_article_num.value;
            if(article_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_article.article_num = article_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_article_num.value = '';
		}.bind(this));
        //设置当前帖子url执行次数
        this.id.set.btn_article_run_num.addEventListener("click",function(e){
            //获取当前帖子序号
            var article_run_num = this.id.set.et_article_run_num.value;
            if(article_run_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_article.article_run_num = article_run_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_article_run_num.value = '';
		}.bind(this));
        //设置当前帖子url执行次数
        this.id.set.btn_article_change_num.addEventListener("click",function(e){
            //获取帖子url当前账号序号
            var article_change_num = this.id.set.et_article_change_num.value;
            if(article_change_num == ''){
                alert("不能为空");
                return;
            }
			this.config.config_article.article_change_num = article_change_num;
            this.setLocalConfigByConfig();
            //alert("设置成功");
            //清空编辑框
            //this.id.set.et_article_change_num.value = '';
		}.bind(this));
        this.id.home.btn_start.addEventListener("click",function(e){
              //console.log("home_btn_start");
              //开启定时评论
              if(this.commentTimer){
                  alert("任务已存在不可以再次执行");
                  return;
              }
              //修改运行状态
              this.config.run_state = 1;
              this.setLocalConfigByConfig();
              //执行评论
              this.comment();
          }.bind(this));
        this.id.home.btn_pause.addEventListener("click",function(e){ 
              //修改状态运行
              this.config.run_state = 0;
              this.setLocalConfigByConfig();
              //暂停评论
              if(this.commentTimer){
                  clearInterval(this.commentTimer);
                  this.commentTimer = null;
                  //获取当前时间
                  //this.config.last_run_time = this.getCurrentTimeStr();
                  var date = new window.Date();
                  this.config.last_run_time = date.getTime();
                  this.setLocalConfigByConfig();
                  alert("停止成功!");
                  return;
              }
              alert("没有定时任务，不要乱停止哦!");
          }.bind(this));
        this.id.set.btn_pause_start.addEventListener("click",function(e){
            //设置暂停开始时间
            //获取开始时间
            var hour = this.id.set.et_pause_start_hour.value;
            var minute = this.id.set.et_pause_start_minute.value;
            if(hour && minute && hour == '' && minute == ''){
                alert("不能为空");
                return;
            }
            this.config.config_pause.pause_start = hour+":"+minute;
            this.setLocalConfigByConfig();
            console.log(this.config.config_pause.pause_start);
        }.bind(this));
        this.id.set.btn_pause_end.addEventListener("click",function(e){
            //设置暂停结束时间
            //获取结束时间
            var hour = this.id.set.et_pause_end_hour.value;
            var minute = this.id.set.et_pause_end_minute.value;
            if(hour && minute && hour == '' && minute == ''){
                alert("不能为空");
                return;
            }
            this.config.config_pause.pause_end = hour+":"+minute;
            this.setLocalConfigByConfig();
            console.log(this.config.config_pause.pause_end);
        }.bind(this));
        this.id.add.btn_account_delete.addEventListener("click",function(e){
            //清空账号
            this.config.config_account.accounts = [];
            this.setLocalConfigByConfig();
            this.refreshAccountList();
        }.bind(this));
        this.id.add.btn_comment_delete.addEventListener("click",function(e){
            //清空账号
            this.config.config_comment.comments = [];
            this.setLocalConfigByConfig();
            this.refreshCommentList();
        }.bind(this));
        add_btn_comment_delete
        this.id.add.btn_article_delete.addEventListener("click",function(e){
            //清空账号
            this.config.config_article.articles = [];
            this.setLocalConfigByConfig();
            this.refreshArticleList();
        }.bind(this));
      //console.log("configviewend");
        this.showHomeInfo();
        
    };
	this.bindView = function(){
		this.id = 
        {
			face : {
				//图标界面
				icon_face_mainFace : document.getElementById("icon_face_mainFace"),
				//总界面
				in_face_mainFace : document.getElementById("in_face_mainFace"),
				//home界面
				home_face_mainFace : document.getElementById("home_face_mainFace"),
				//添加界面
				add_face_mainFace : document.getElementById("add_face_mainFace"),
				//设置界面
				set_face_mainFace : document.getElementById("set_face_mainFace"),
			},
			nav : {
				//倒计时文本
				btn_home : document.getElementById("nav_btn_home"),
				//倒计时文本
				btn_add : document.getElementById("nav_btn_add"),
				//倒计时文本
				btn_set : document.getElementById("nav_btn_set")
			},
			home : {
				//倒计时文本
				tv_countdown : document.getElementById("home_tv_countdown"),
				//开始按钮
				btn_start : document.getElementById("home_btn_start"),
				//暂停按钮
				btn_pause : document.getElementById("home_btn_pause"),
				//当前账号文本
				tv_account : document.getElementById("home_tv_account"),
				//当前评论文本
				tv_comment : document.getElementById("home_tv_comment"),
				//当前帖子文本
				tv_article : document.getElementById("home_tv_article"),
				//暂停开始时间文本
				tv_stoptime_start : document.getElementById("home_tv_stoptime_start"),
				//暂停结束时间文本
				tv_stoptime_end : document.getElementById("home_tv_stoptime_end")
			},
			add : {
                //账号列表ul节点
                face_accountul : document.getElementById("add_face_accountul"),
				//账号编辑框
				et_account : document.getElementById("add_et_account"),
				//token编辑框
				et_token : document.getElementById("add_et_token"),
				//账号添加按钮
				btn_account_add : document.getElementById("add_btn_account_add"),
				//账号删除按钮
				btn_account_delete : document.getElementById("add_btn_account_delete"),
              
                //评论列表ul节点
                face_commentul : document.getElementById("add_face_commentul"),
				//评论编辑框
				et_comment : document.getElementById("add_et_comment"),
				//评论添加按钮
				btn_comment_add : document.getElementById("add_btn_comment_add"),
				//评论删除按钮
				btn_comment_delete : document.getElementById("add_btn_comment_delete"),
              
                //帖子列表ul节点
                face_articleul : document.getElementById("add_face_articleul"),
				//帖子url编辑框
				et_article : document.getElementById("add_et_article"),
				//帖子url添加按钮
				btn_article_add : document.getElementById("add_btn_article_add"),
				//帖子url删除按钮
				btn_article_delete : document.getElementById("add_btn_article_delete")
			},
			set : {
                //评论 间隔时间编辑框
                et_run_interval_time : document.getElementById("set_et_run_interval_time"),
                //评论间隔时间按钮
                btn_run_interval_time : document.getElementById("set_btn_run_interval_time"),
				//账号序号编辑框
				et_account_num : document.getElementById("set_et_account_num"),
				//设置账号序号按钮
				btn_account_num : document.getElementById("set_btn_account_num"),
				//账号执行次数编辑框
				et_account_run_num : document.getElementById("set_et_account_run_num"),
				//设置账号执行次数按钮
				btn_account_run_num : document.getElementById("set_btn_account_run_num"),
				//账号更换次数编辑框
				et_account_change_num : document.getElementById("set_et_account_change_num"),
				//设置账号更换次数按钮
				btn_account_change_num : document.getElementById("set_btn_account_change_num"),
				//评论序号编辑框
				et_comment_num : document.getElementById("set_et_comment_num"),
				//设置评论序号按钮
				btn_comment_num : document.getElementById("set_btn_comment_num"),
				//评论执行次数编辑框
				et_comment_run_num : document.getElementById("set_et_comment_run_num"),
				//设置评论执行次数按钮
				btn_comment_run_num : document.getElementById("set_btn_comment_run_num"),
				//评论更换次数编辑框
				et_comment_change_num : document.getElementById("set_et_comment_change_num"),
				//设置评论更换次数按钮
				btn_comment_change_num : document.getElementById("set_btn_comment_change_num"),
				//帖子序号编辑框
				et_article_num : document.getElementById("set_et_article_num"),
				//设置帖子序号按钮
				btn_article_num : document.getElementById("set_btn_article_num"),
				//帖子执行次数编辑框
				et_article_run_num : document.getElementById("set_et_article_run_num"),
				//设置帖子执行次数按钮
				btn_article_run_num : document.getElementById("set_btn_article_run_num"),
				//帖子更换次数编辑框
				et_article_change_num : document.getElementById("set_et_article_change_num"),
				//设置帖子更换次数按钮
				btn_article_change_num : document.getElementById("set_btn_article_change_num"),
                //暂停开始时间编辑框：时
				et_pause_start_hour : document.getElementById("set_et_pause_start_hour"),
                //暂停开始时间编辑框：分
				et_pause_start_minute : document.getElementById("set_et_pause_start_minute"),
				//暂停开始时间按钮
				btn_pause_start : document.getElementById("set_btn_pause_start"),
				//暂停开始时间编辑框：时
				et_pause_end_hour : document.getElementById("set_et_pause_end_hour"),
                //暂停开始时间编辑框：分
				et_pause_end_minute : document.getElementById("set_et_pause_end_minute"),
				//暂停结束时间按钮
				btn_pause_end : document.getElementById("set_btn_pause_end")
			},
            other : {
                //评论编辑框
                et_comment : document.getElementById("last"),
                //提交评论按钮
                btn_submit_comment : document.querySelector("input[value='发送']"),
                //登陆按钮
                btn_login : document.querySelector(".a_show_login[href='/accounts/register?reason=discuss']"),
            }
        };
	};
    this.initConfig = function(){
        //获取本地存储的对象
        var config = window.localStorage.config;
        if(!config || config == ''){
            config = '{'+
                    '"config_account":{"accounts":[],"account_num":1,"account_run_num":0,"account_change_num":3},'+
                    '"config_comment":{"comments":[],"comment_num":1,"comment_run_num":0,"comment_change_num":3},'+
                    '"config_article":{"articles":[],"article_num":1,"article_run_num":0,"article_change_num":3},'+
                    '"config_pause":{"pause_start":"","pause_end":""},'+
                    '"run_state" : 0,'+//默认状态为0；0：未运行，1：运行中，2：暂停中
                    '"last_run_time" : "",'+//上次运行时间
                    '"run_interval_time" : 600'+
            '}';
        }
        this.setConfig(config);
    };
    this.setConfig = function(config){
        console.log("config:",config);
        var configObj = eval("("+config+")");
        this.config = configObj;
        window.localStorage.config = config;
        if(this.config.run_interval_time){
            console.log("存在")
            this.time = this.config.run_interval_time;
        }else {
            this.time = 10000;
            console.log("不存在");
        }
        console.log("this.time:",this.config.run_interval_time);
        
    };
    this.setLocalConfigByConfig = function(){
        window.localStorage.config = JSON.stringify(this.config);
    };
    this.addAccount = function(account){
        this.config.config_account.accounts.push(account);
        this.setLocalConfigByConfig();
    };
    this.deleteAccount = function(index){
        this.config.config_account.accounts.splice(index,1);
        this.setLocalConfigByConfig();
    };
    this.refreshAccountList = function(){
        //删除ul下的子节点
        var childs = this.id.add.face_accountul.childNodes;
        if(!childs){
            childs = 0;
        }
        for(var i = 0,len = childs.length;i < len;i ++){
          this.id.add.face_accountul.removeChild(childs[0]);
          console.log("删除",i,"次");
        }
        //获取用户数据
        console.log(this.config);
        var userJson = this.config.config_account.accounts;
        console.log(userJson);
        for(var i = 0;i < userJson.length;i ++){
          //创建li标签
          var li = document.createElement("li");
          console.log(userJson[i]);
          li.innerText = i+":account:"+userJson[i].account+" token:"+userJson[i].token;
          //添加到ul上
          this.id.add.face_accountul.appendChild(li);
        }
    };
    this.refreshCommentList = function(){
          //删除ul下的子节点
          var childs = this.id.add.face_commentul.childNodes;
          if(!childs){
              childs = 0;
          }
          for(var i = 0,len = childs.length;i < len;i ++){
            this.id.add.face_commentul.removeChild(childs[0]);
            console.log("删除",i,"次");
          }
          //获取用户数据
          console.log(this.config);
          var commentJson = this.config.config_comment.comments;
          console.log(commentJson);
          for(var i = 0;i < commentJson.length;i ++){
            //创建li标签
            var li = document.createElement("li");
            console.log(commentJson[i]);
            li.innerText = i+":"+commentJson[i];
            //添加到ul上
            this.id.add.face_commentul.appendChild(li);
          }
    };
    this.refreshArticleList = function(){
          //删除ul下的子节点
          var childs = this.id.add.face_articleul.childNodes;
          if(!childs){
              childs = 0;
          }
          for(var i = 0,len = childs.length;i < len;i ++){
            this.id.add.face_articleul.removeChild(childs[0]);
            console.log("删除",i,"次");
          }
          //获取用户数据
          console.log(this.config);
          var articleJson = this.config.config_article.articles;
          console.log(articleJson);
          for(var i = 0;i < articleJson.length;i ++){
            //创建li标签
            var li = document.createElement("li");
            console.log(articleJson[i]);
            li.innerText = i+":"+articleJson[i];
            //添加到ul上
            this.id.add.face_articleul.appendChild(li);
          }
    };
    this.comment = function(){

        //2.判断帖子url执行次数是否正常
        if(this.config.config_article.article_run_num < 1){
            //否：设为0
            this.config.config_article.article_run_num = 1;
        }
        //3.判断更换次数是否正常
        if(this.config.config_article.article_change_num < 1){
            //否：设为3
            this.config.config_article.article_change_num = 3;
        }
        //4.（更换帖子）是否需要更换帖子
        if(this.config.config_article.article_run_num > this.config.config_article.article_change_num){
            //判断帖子url序号是否为最后一个
            if(this.config.config_article.article_num >= this.config.config_article.articles.length){
                //是：置1     
                this.config.config_article.article_num = 1;          
                
            }
            else {
                //否：向下取   
                this.config.config_article.article_num ++;
            }
            this.config.config_article.article_run_num = 1;
            //alert("second");
            this.setLocalConfigByConfig();
            window.location.href = this.config.config_article.articles[this.config.config_article.article_num - 1];
            return;
        }
        //判断当前目标url是否正常
        if(this.config.config_article.article_num < 0 || this.config.config_article.article_num > this.config.config_article.articles.length){
            this.config.config_article.article_num = 1;
        }
      
      
        //是否当前在目标url页面上
        var locationUrl = window.location.href;
        if(locationUrl != this.config.config_article.articles[this.config.config_article.article_num - 1]){
            //alert("first");
            window.location.href = this.config.config_article.articles[this.config.config_article.article_num - 1];
            return;
        }
        //如果上次执行时间和当前时间间隔了一个小时以上，那就将账号的执行次数设为1
        if(this.config.last_run_time && this.config.last_run_time != ''){
          var date = new window.Date();
          if(date.getTime() - this.config.last_run_time > 3600000){
              this.config.config_account.account_run_num = 1;
          }
        }
        //如果编辑框和发送按钮都没有的话，就代表没有加入该组
        if(!this.id.other.btn_submit_comment || !this.id.other.et_comment){
            alert("请先将账号加入该组，才能进行评论");
            return;
        }
        //评论流程，逻辑
        //账号：
        //1.判断账号是否为空，
        if(this.config.config_account.accounts.length <= 0){
            //是：直接返回，定时任务置空，执行状态设为”未开始“
            //修改状态运行
            this.config.run_state = 0;
            this.setLocalConfigByConfig();
            this.id.home.tv_countdown = "未开始";
            if(this.commentTimer){
                clearInterval(this.commentTimer);
                this.commentTimer = null;
            }

            alert("账号不能为空");
            return;
        }
        //2.判断账号执行次数是否正常
        if(this.config.config_account.account_run_num < 1){
            //否设置为1
            this.config.config_account.account_run_num = 1;
        }          
        //3.判断更换次数是否正常
        if(this.config.config_account.account_change_num < 1){
            //否：设为3
            this.config.config_account.account_change_num = 3;
        }           
        //4.判断是否需要更换账号
        if(this.config.config_account.account_run_num > this.config.config_account.account_change_num){
            //判断账号序号是否为最后一个
            if(this.config.config_account.account_num >= this.config.config_account.accounts.length){
                //是：则置1               
                this.config.config_account.account_num = 1;
            }
            else{
                this.config.config_account.account_num ++;
                //alert(this.config.config_account.account_num);
            }
            this.config.config_account.account_run_num = 1;
            var cookie = this.config.config_account.accounts[this.config.config_account.account_num - 1].token;
            setCookie("dbcl2",cookie);
            this.setLocalConfigByConfig();
            window.location.href = window.location.href;
            return;
        }
        //判断当前账号是否正常
        if(this.config.config_account.account_num < 1 || this.config.config_account.account_num > this.config.config_account.accounts.length){
            this.config.config_account.account_num = 1;
        }
        
        //评论：
        var comment = '';
        //1.判断评论是否为空
        if(this.config.config_comment.comments.length < 1){
            //是：给其设置默认评论
            comment = '顶顶';
        }
        
        //2.判断评论执行次数是否正常
        if(this.config.config_comment.comment_run_num < 1){
            //否：设为1
            this.config.config_comment.comment_run_num = 1;
        }
        //3.判断更换次数是否正常
        if(this.config.config_comment.comment_change_num < 1){
            //否：设为3
            this.config.config_comment.comment_change_num = 3;
        }
        //4.（更换评论）是否需要更换评论
        if(this.config.config_comment.comment_run_num > this.config.config_comment.comment_change_num){
            //判断评论序号是否为最后一个
            if(this.config.config_comment.comment_num >= this.config.config_comment.comments.length){
                //是：置1
                
                this.config.config_comment.comment_num = 1;
            }
            else {
                //否：向下取
                this.config.config_comment.comment_num ++;
            }
            this.config.config_comment.comment_run_num = 1;
        }
        //判断当前评论
        if(this.config.config_comment.comment_num < 1 || this.config.config_comment.comment_num > this.config.config_comment.comments.length){
            this.config.config_comment.comment_num = 1;
        }
            
        //帖子url：  
        //1.判断帖子url是否为空
        if(this.config.config_article.articles.length < 1){
            //是：直接返回，提示要求填写目标url
            //修改状态运行
            this.config.run_state = 0;
            this.setLocalConfigByConfig();
            if(this.commentTimer){
                clearInterval(this.commentTimer);
                this.commentTimer = null;
            }
            alert("目标帖子的url不能为空");
            return;
        }
      
        
      
        //通过是否显示评论编辑框来判断是否登陆
        if(this.id.other.btn_login){
            //设置cookie，进行登陆
            //获取当前账号的token
            var cookie = this.config.config_account.accounts[this.config.config_account.account_num - 1].token;
            setCookie("dbcl2",cookie);
            //刷新页面
            window.location.href = window.location.href;
            return;
        }
        //5.获取到了，账号，评论内容，目标url，
        //并设置好了账号，填充好了内容，目标url
        console.log("comment:",comment);
        console.log("num:",this.config.config_comment.comment_num);
        if(comment && comment != ''){
            this.id.other.et_comment.value = comment;
        }else {
            this.id.other.et_comment.value = this.config.config_comment.comments[this.config.config_comment.comment_num - 1];
        }
        //等待定时任务完毕，
        //判断如果存在开始时间和结束时间，
        if(this.config.config_pause.pause_start && this.config.config_pause.pause_end && this.config.config_pause.pause_start != '' && this.config.config_pause.pause_end != ''){
            //则判断当前时间是否在此段时间之内
            var date = new Date();
            var hour = date.getHours();
            var minute = date.getMinutes();
            var time = hour + ":" + minute;
            if(this.config.config_pause.pause_start <= time && this.config.config_pause.pause_end > time){
                //如果在，则等待指定时间内执行评论
                //判断距离结束时间有多久
                var ts = this.config.config_pause.pause_end.split(":");
                var h = (ts[0] - hour)*1000*60*60;
                var m = (ts[1] - minute)*1000*60;
                this.time = 0;
                console.log(h+m);
                this.commentTimer = setTimeout(this.showCountdown, h+m);
                this.showStopTime();
                return;
            }
        }
        
        this.commentTimer = setInterval(this.showCountdown, 1000);
        
    };
    //中途停止时间
    this.showStopTime = function(){
        this.id.home.tv_countdown.innerText = "将在"+this.config.config_pause.pause_end+"开启运行";
    }.bind(this);
    this.showCountdown = function(){
        this.time --;
        console.log(this.time);
        this.id.home.tv_countdown.innerText = this.time;
        if(this.time < 1){
            console.log("小于1");
            //此处代表将要执行，因此我们准备下次的条件
            //账号执行次数自增
            this.config.config_account.account_run_num ++;
            //评论同理
            this.config.config_comment.comment_run_num ++;
            //目标url同理
            this.config.config_article.article_run_num ++;
            //保存到本地变量中
            this.setLocalConfigByConfig();
            //则执行评论。
            this.id.other.btn_submit_comment.click();
            //clearInterval(this.commentTimer);
        }
    }.bind(this);
    this.initRun = function(){
        //判断是否为运行状态，运行状态则运行定时评论
        if(this.config.run_state == 1){
            //运行中
            //是否有目标url
            if(this.config.config_article.articles.length < 1){
                alert("请添加目标url帖子路径");
                return;
            }
            //是否当前在目标url页面上
            /*var locationUrl = window.location.href;
            if(locationUrl != this.config.config_article.articles[this.config.config_article.article_num - 1]){
                window.location.href = this.config.config_article.articles[this.config.config_article.article_num - 1];
                return;
            }*/
            //是否有账号
            if(this.config.config_account.accounts.length < 1){
                alert("请添加账号");
                return;
            }
            //是否登陆成功
            //通过是否显示评论编辑框来判断是否登陆
            /*if(!this.id.other.btn_submit_comment || !this.id.other.et_comment || this.id.other.btn_login){
                //设置cookie，进行登陆
                //获取当前账号的token
                var cookie = this.config.config_account.accounts[this.config.config_account.account_num - 1].token;
                setCookie("dbcl2",cookie);
                //刷新页面
                window.location.href = window.location.href;
                return;
            }*/
            this.comment();
        }
    };
    this.getCurrentTimeStr =  function(){
        //获取当前时间
        var myDate = new Date();
        var year = myDate.getFullYear();
        var month = myDate.getMonth()+1; 
        var day = myDate.getDate(); 
        var hour = myDate.getHours();
        var minute = myDate.getMinutes();
        var second = myDate.getSeconds();
        return (year < 10 ? "0"+year : year)+"-"+(month < 10 ? "0"+month : month)+"-"+(day < 10 ? "0"+day : day)+" "+
                                      (hour < 10 ? "0"+hour : hour)+":"+(minute < 10 ? "0"+minute : minute)+":"+(second < 10 ? "0"+second : second);
    };
    //展示设置信息
    this.showSetInfo = function(){
        this.id.set.et_run_interval_time.value = this.config.run_interval_time;
          this.id.set.et_account_num.value = this.config.config_account.account_num;
          this.id.set.et_account_run_num.value = this.config.config_account.account_run_num;
          this.id.set.et_account_change_num.value = this.config.config_account.account_change_num;
          this.id.set.et_comment_num.value = this.config.config_comment.comment_num;
          this.id.set.et_comment_run_num.value = this.config.config_comment.comment_run_num;
          this.id.set.et_comment_change_num.value = this.config.config_comment.comment_change_num;
          this.id.set.et_article_num.value = this.config.config_article.article_num;
          this.id.set.et_article_run_num.value = this.config.config_article.article_run_num;
          this.id.set.et_article_change_num.value = this.config.config_article.article_change_num;
          var startTime = this.config.config_pause.pause_start.split(":");
          this.id.set.et_pause_start_hour.value = startTime[0];
          this.id.set.et_pause_start_minute.value = startTime[1];
          var endTime = this.config.config_pause.pause_end.split(":");
          this.id.set.et_pause_end_hour.value = endTime[0];
          this.id.set.et_pause_end_minute.value = endTime[1];
    };
    //展示首页信息
    this.showHomeInfo = function(){
        if(this.config.config_account.accounts && this.config.config_account.accounts.length > 0){
            this.id.home.tv_account.innerText = this.config.config_account.accounts[this.config.config_account.account_num -1].account;
        }
        //console.log(this.config.config_account.accounts[this.config.config_account.account_num -1]);
        if(this.config.config_comment.comments && this.config.config_comment.comments.length > 0){
             this.id.home.tv_comment.innerText = this.config.config_comment.comments[this.config.config_comment.comment_num -1];
        }
         
        if(this.config.config_article.articles && this.config.config_article.articles.length > 0){
            this.id.home.tv_article.innerText = this.config.config_article.articles[this.config.config_article.article_num -1];
        }
          
        if(this.config.config_pause.pause_start && this.config.config_pause.pause_start != ''){
            this.id.home.tv_stoptime_start.innerText = this.config.config_pause.pause_start;
        }
            
        if(this.config.config_account.accounts && this.config.config_pause.pause_end != ''){
            this.id.home.tv_stoptime_end.innerText = this.config.config_pause.pause_end;
        }
    }
}
  
  
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*240*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

//获取body
var body = document.querySelector("body");
var face = new Face();
//console.log(face);
face.addView(body);
face.bindView();
console.log("bindView");
face.initConfig();
console.log("initConfig");
face.configView();
console.log("configView");
face.initRun();

